<template>
  <div class="products-container">
    <el-row :gutter="20">
      <!-- 筛选区域 -->
      <el-col :span="24" class="filter-section">
        <el-card>
          <el-form :inline="true" :model="filterForm">
            <el-form-item label="分类">
              <el-select v-model="filterForm.category" placeholder="选择分类" clearable>
                <el-option
                  v-for="category in productStore.categories"
                  :key="category"
                  :label="category"
                  :value="category"
                />
              </el-select>
            </el-form-item>
            
            <el-form-item label="价格区间">
              <el-input-number
                v-model="filterForm.minPrice"
                :min="0"
                placeholder="最低价"
                style="width: 130px"
              />
              <span class="mx-2">-</span>
              <el-input-number
                v-model="filterForm.maxPrice"
                :min="0"
                placeholder="最高价"
                style="width: 130px"
              />
            </el-form-item>
            
            <el-form-item>
              <el-button type="primary" @click="handleFilter">筛选</el-button>
              <el-button @click="resetFilter">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
      
      <!-- 商品列表 -->
      <el-col :span="24" class="product-list">
        <el-row :gutter="20">
          <el-col
            v-for="product in productStore.products"
            :key="product.id"
            :xs="24"
            :sm="12"
            :md="8"
            :lg="6"
            class="product-item"
          >
            <el-card :body-style="{ padding: '0px' }" class="product-card">
              <img :src="product.image" class="product-image" />
              <div class="product-info">
                <h3>{{ product.name }}</h3>
                <p class="product-description">{{ product.description }}</p>
                <div class="product-price">¥{{ product.price }}</div>
                <div class="product-actions">
                  <el-button type="primary" @click="viewProduct(product.id)">
                    查看详情
                  </el-button>
                  <el-button
                    type="success"
                    @click="addToCart(product.id)"
                    :disabled="!userStore.token"
                  >
                    加入购物车
                  </el-button>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { onMounted, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useProductStore } from '../stores/product'
import { useUserStore } from '../stores/user'
import { useCartStore } from '../stores/cart'
import { ElMessage } from 'element-plus'

const router = useRouter()
const productStore = useProductStore()
const userStore = useUserStore()
const cartStore = useCartStore()

const filterForm = reactive({
  category: '',
  minPrice: null,
  maxPrice: null
})

onMounted(async () => {
  await productStore.fetchCategories()
  await productStore.fetchProducts()
})

const handleFilter = async () => {
  await productStore.fetchProducts(
    filterForm.category,
    filterForm.minPrice,
    filterForm.maxPrice
  )
}

const resetFilter = async () => {
  filterForm.category = ''
  filterForm.minPrice = null
  filterForm.maxPrice = null
  await productStore.fetchProducts()
}

const viewProduct = (id) => {
  router.push(`/product/${id}`)
}

const addToCart = async (productId) => {
  if (!userStore.token) {
    ElMessage.warning('请先登录')
    router.push('/login')
    return
  }
  
  await cartStore.addToCart(productId)
  ElMessage.success('已添加到购物车')
}
</script>

<style scoped>
.products-container {
  padding: 20px;
}

.filter-section {
  margin-bottom: 20px;
}

.product-list {
  margin-top: 20px;
}

.product-item {
  margin-bottom: 20px;
}

.product-card {
  height: 100%;
  transition: transform 0.3s;
}

.product-card:hover {
  transform: translateY(-5px);
}

.product-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.product-info {
  padding: 14px;
}

.product-description {
  color: #666;
  margin: 10px 0;
  height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.product-price {
  color: #f56c6c;
  font-size: 20px;
  font-weight: bold;
  margin: 10px 0;
}

.product-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
}

.mx-2 {
  margin: 0 8px;
}
</style> 